package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {

    @Autowired
    private YddCateService yddCateService;

    /**
     * 分类详情
     * @param id
     * @return
     */
 /*   @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        log.info("分类详情 id{}",id);
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddCate::getId,id);
        List<YddCate> list = yddCateService.list(queryWrapper);

        return new ResultData(0,"",list);
    }*/

    /**
     * 分类列表-分页查询
     * @param page
     * @param limit
     * @param catename
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Long page, Long limit,String catename){
        log.info("分页查询 page {}",page,
                "pageSize {}",limit,
                "name {}",catename);

        //分页构造器
        Page<YddCate> pageInfo = new Page<>(page,limit);

        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件,根据name进行like模糊查询
        queryWrapper.like(catename != "",YddCate::getCatename,catename);
        //添加排序条件，根据数字降序排列
        queryWrapper.orderByDesc(YddCate::getSort_num);
        Page<YddCate> yddCatePage = yddCateService.page(pageInfo, queryWrapper);

        //将数据封装到PageResult
        PageResult pageResult = PageResult.init(yddCatePage.getTotal(), limit, page,  yddCatePage.getRecords());

        log.info("    {}   ",pageResult);
        return ResultData.ok("",pageResult);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id){
        log.info("删除分类 id {}",id);
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddCate::getId,id);
        boolean flag = yddCateService.remove(queryWrapper);

        return ResultData.ok("",flag);
    }


    /**
     * 根据id查询数据并回显
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(long id){
        YddCate cate = yddCateService.getById(id);
        return ResultData.ok("",cate);
    }


    /**
     * 修改分类
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate){
        boolean updateById = yddCateService.updateById(yddCate);

        return ResultData.ok("",updateById);

    }

    /**
     * 分类添加
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddCate yddCate){
        log.info("添加分类 yddCate{}",yddCate);
        boolean save = yddCateService.save(yddCate);

        return ResultData.ok("",save);
    }

    /**
     * 分类列表-全部
     * @return
     */
    @GetMapping("all")
    public ResultData all(){
        List<YddCate> list = yddCateService.list();
        return ResultData.ok("",list);
    }
}
